package com.livestocksolutions.ui;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

/* -- Version History --
 *  1.0 : 10 September 2012 - Initial version.
 */

/**
 * A base class to create the layout for a Join Object
 * @author Adam Parr
 * @version 1.0
 * 
 * @.modified 19 October 2012
 */
public class ViewJoinArrayAdapter extends BaseArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 5;
	/**
	 * The area for each position within the String[position][{@value #ID}] array passed in the
	 * Constructor.
	 */
	public static final int ID = 0;
	/**
	 * The Date the Bull was placed into the Join position within the 
	 * String[position][{@value #INSERTIONDATE}] array passed in the Constructor.
	 */
	public static final int INSERTIONDATE = 1;
	/**
	 * The Date the Bull was removed from the Join position within the 
	 * String[position][{@value #REMOVALDATE}] array passed in the Constructor.
	 */
	public static final int REMOVALDATE = 2;
	/**
	 * The number of cows that are in this Join position within the 
	 * String[position][{@value #COUNT}] array passed in the Constructor.
	 */
	public static final int COUNT = 3;
	/**
	 * The Name of the Bull that was placed into this Join position within the 
	 * String[position][{@value #BULL}] array passed in the Constructor.
	 */
	public static final int BULL = 4;
	/**
	 * Name position within the String[index][{@value #NAME}] array that are set in
	 * {@link #setBullList(String[][])}.
	 */
	public static final int NAME = 1;
	
	/**
	 * An array containing a list of possible Bulls to add to this Join.
	 */
	private String[][] bulls;
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Joining data to represent in the ListView.
	 */
	public ViewJoinArrayAdapter(BaseActivity context, String[] values) {
		super(context, new String[][]{values});
	}
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Joining data to represent in the ListView.
	 */
	protected ViewJoinArrayAdapter(BaseActivity context, String[][] values) {
		super(context, values);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		setView(R.layout.item_join);
		View rowView = super.getView(position, convertView, parent);
		
		TextView joinEntry = (TextView) rowView.findViewById(R.id.joinEntry);
		TextView joinExit = (TextView) rowView.findViewById(R.id.joinExit);
		Button joinBull = (Button) rowView.findViewById(R.id.joinBull);
		
		values[position][INSERTIONDATE]=values[position][INSERTIONDATE]!=null && values[position][INSERTIONDATE].length()>0?values[position][INSERTIONDATE]	:"";
		values[position][ REMOVALDATE ]=values[position][ REMOVALDATE ]!=null && values[position][ REMOVALDATE ].length()>0?values[position][ REMOVALDATE ]	:"";

		String s = context.format(context.parse(values[position][INSERTIONDATE]));
		joinEntry.setText(!s.equals("")?s:context.getResources().getString(R.string.message_noText,context.getResources().getString(R.string.message_notSet)));
		s = context.format(context.parse(values[position][REMOVALDATE]));
		joinExit.setText(!s.equals("")?s:context.getResources().getString(R.string.message_noText,context.getResources().getString(R.string.message_notSet)));
		
		// Determine the Bull of this Join
		for(int x=0; x < bulls.length; x++){
			if(values[position][BULL]==null || bulls[x][ID].equals(values[position][BULL])){
				((TextView) joinBull).setText(bulls[x][NAME]);
				break;
			}
		}
		
		return rowView;
	}
	
    /**
	 * Set the list of Bulls that this Join could belong to.<br>
	 * The structure of this list is expected to be the First Dimension containing each Bull,
	 * the Second Dimension is structured with {@link #ID} and {@link #NAME}, with ID containing
	 * a String representation of the long id of that Bull, and NAME containing the name of the
	 * Bull.
	 * @param bullList The String Array to use as the list of mobs.
	 */
	public void setBullList(String[][] bullList){
		bulls = bullList;
	}
}
